package com.echo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.activeandroid.query.Select;
import com.echo.activity.HitoryDayRecordActivity;
import com.echo.activity.R;
import com.echo.activity.SportsActivity;
import com.echo.adapter.HistoryRecordAdapter;
import com.echo.model.DayData;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by EchoGe on 2016/6/4.
 */
public class HistoryAllRecordsFragment extends HistoryChartsFragment {

    private View mView;

    private PtrFrameLayout mPtrFrame;
    private RecyclerView mRecordRecycler;
    private HistoryRecordAdapter mAdapter;

    private List mDayData=new ArrayList();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //mParentActivity = (SportsActivity) getActivity();
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_history_all_record, container, false);
            initView(mView);
        }

        //initViewPager();
        loadData();

        return mView;
    }

    private void initView(View mView) {
        mPtrFrame= (PtrFrameLayout) mView.findViewById(R.id.history_ptr_frame);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                loadData();
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrame.refreshComplete();
                    }
                },1800);
            }
        });
        mRecordRecycler= (RecyclerView) mView.findViewById(R.id.record_recycler);
        mRecordRecycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false));
    }

    @Override
    public void loadData() {
        mDayData=new Select().from(DayData.class).orderBy("date asc").execute();
        mAdapter=new HistoryRecordAdapter(getActivity(),mDayData);
        mAdapter.setOnRecordClickListener(new HistoryRecordAdapter.OnRecordClickListener() {
            @Override
            public void onRecordDetail(HistoryRecordAdapter.ViewHolder viewHolder) {
                Intent intent=new Intent();
                intent.setClass(getActivity(), HitoryDayRecordActivity.class);
                Bundle bundle =new Bundle();
                bundle.putString("date", viewHolder.date);
                intent.putExtra("key", bundle);
                startActivity(intent);
            }
        });
        mRecordRecycler.setAdapter(mAdapter);
    }
}
